package data.base;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;

public class Material {
  public static Material DEFAULT = new Material("default");
  
  private RenderColor color = new RenderColor(1);
  private BufferedImage texture;
  private String name;
  
  public Material(String name){
    this.name = name;
  }
  
  public RenderColor getRenderColorAt(TextureCoordinate tc) {
    if(tc == null || texture == null){
      return color;
    }
    else{
      return new RenderColor(new Color(texture.getRGB(
          (int)Math.round(tc.getU() * texture.getWidth()), 
          (int)Math.round((1 - tc.getV()) * texture.getHeight()))));
    }
  }

  public Color getColorAt(TextureCoordinate tc) {
    if(tc == null || texture == null){
      return color.getColor();
    }
    else{
      return new Color(texture.getRGB(
          (int)Math.round(tc.getU() * texture.getWidth()), 
          (int)Math.round((1 - tc.getV()) * texture.getHeight())));
    }
  }

  public void setColor(Color c) {
    color = new RenderColor(c);
  }

  public BufferedImage getTexture() {
    return texture;
  }

  public void setTexture(String file) {
    Image img = new ImageIcon(file).getImage();
    texture = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
    Graphics g = texture.getGraphics();
    g.drawImage(img, 0, 0, null);
    g.dispose();
  }
  
  public String getName(){
    return name;
  }

}
